﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Automation.Protocols.Dao;
using Automation.Protocols.Model;

using Common.Logging;
using SupportLibrary.Message;
using SupportLibrary;
namespace Automation.Dal.Excel.Dao
{
    public class TestCaseDao : TestCaseBase
    {   
        private ExcelProvider _exProvider;
        private ILog log = LogManager.GetLogger(typeof(TestCaseDao));
        private string _Path;
        public TestCaseDao(string path)
        {
            _Path = path;
            _exProvider = ExcelProvider.Create(@path);
            if (_exProvider == null)
            {     //MessageShow.FileDoesNotExist(path);
                log.Debug(Resources.ErrorMessage.ReadTCaseSheetErr);
                throw new Exception(path + Resources.ErrorMessage.ReadTCaseSheetErr);
            }
        }

        public override List<TestCase> getTestcases()
        {             
            List<TestCase> list = new List<TestCase>();
            log.Debug(Resources.Information.msg_ReadingSheetTCase);
           
            
            try
            {
                if (_exProvider != null)
                {
                    var TestCases = _exProvider.GetSheet<Automation.Dal.Excel.Model.TestCase>();
                    int i = 0;
                    if (TestCases != null)
                    {
                        foreach (Automation.Dal.Excel.Model.TestCase item in TestCases)
                        {
                            if (item.ID != null)
                            {
                                i++;
                                if (item.isValid())
                                {
                                    int n = list.Count;
                                    for (int j = 0; j < n; j++)
                                    {
                                        if (list[j].Name.Equals(item.Name))
                                        {
                                            //MessageShow.TCaseNameExist(item.Name);
                                            throw new Exception("Testcase "+item.Name+" doesnot exist!");
                                            //return null;
                                        }
                                    }
                                    list.Add(new TestCase() { 
                                        ID = item.ID,
                                        Name = item.Name,
                                        Behavior = item.Behavior,
                                        Parameters = item.Parameters,
                                        State = item.State
                                    });
                                }
                                else
                                {
                                    //MessageShow.InvalidRecord(i + 1);
                                    log.Debug(Resources.ErrorMessage.ReadTCaseSheetErr );
                                    throw new Exception("Record "+(i+1)+" is invalid!");
                                }
                            }
                        }
                    }
                   
                    log.Debug(Message.ReplaceMessage(Resources.Information.msg_NumofRecord,ByKey.ByNumber(),i.ToString()));

                }
            }
            catch (Exception e)
            {
                //MessageShow.ExceptionOccur("Read TCase sheet exception.", e);
                log.Error(Resources.ErrorMessage.ReadTCaseSheetErr+ e.GetType().ToString() + e.Message);
                throw new Exception(Resources.ErrorMessage.ReadTCaseSheetErr + e.GetType().ToString());
            }

            return list;
        }

        public override string getTestString()
        {
            return "Reflection test!";
        }
    }
}
